// pages/message/sendMessage.js
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: '',
    focus:false,
    senderUid: wx.getStorageSync('userinfo').uid,
    avatar: '',
    alias: '',
    selfAvatar: '',
    message: '',
    messageList: [],
    toView: '',
    windowWidth: 0,
    windowHeight: 0,
    InputBottom: 0,
    test: wx.getSystemInfoSync().windowHeight - 115
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData({
      uid: options.uid,
      avatar: options.avatar,
      alias: options.alias,
      senderUid: wx.getStorageSync('userinfo').uid,
      selfAvatar: wx.getStorageSync('userinfo').avatar,
      selfAlias: wx.getStorageSync('userinfo').alias,
      windowWidth: wx.getSystemInfoSync().windowWidth,
      windowHeight: wx.getSystemInfoSync().windowHeight,
      keyHeight: 0,
      InputBottom: 0,
      formatMessage: {},
      test: wx.getSystemInfoSync().windowHeight - 115
    })
    wx.setNavigationBarTitle({
      title: options.alias + ''
    })
    // 添加联系人
    wx.User.addContact({
      data: {
        uid: this.data.senderUid,
        otherUid: this.data.uid,
        avatar: this.data.avatar,
        alias: this.data.alias,
        myAvatar: this.data.selfAvatar,
        myAlias: this.data.selfAlias
      }
    })
    await wx.User.listMessages({
      data: {
        senderUid: this.data.senderUid,
        recevierUid: this.data.uid
      }
    })
    this.setData({
      messageList: wx.getStorageSync('messageList'),
      toView: 'msg-' + (wx.getStorageSync('messageList').length - 1)
    })
    await wx.User.readMessages({
      data: {
        senderUid: this.data.senderUid,
        recevierUid: this.data.uid
      }
    })
  },
  //失去聚焦(软键盘消失)
  blur: function (e) {
    console.log("点击了blur，键盘消失", e)
    this.setData({
      test: this.data.windowHeight - 115,
      InputBottom: 0,
    })
  },
  focus: function (e) {
    console.log("点击了focus键盘出现", e)
    this.setData({
      test: this.data.windowHeight - e.detail.height - 115,
      InputBottom: e.detail.height,
    })
    var that = this
    setTimeout( function() {
      that.setData({
        toView: 'msg-' + (wx.getStorageSync('messageList').length - 1)
      },500)
    })
  },
  messageIn(e) {
    this.setData({
      message: e.detail.value
    })
  },
  async sendOne() {
    this.setData({
      focus:true,
      toView: 'msg-' + (wx.getStorageSync('messageList').length - 1)
    })
    await wx.User.sendOne({
      data: {
        message: this.data.message,
        uid: this.data.uid,
        senderUid: this.data.senderUid
      }
    })
    var time = util.formatTime(new Date()).substring(11);
    this.data.formatMessage.senderUid = this.data.senderUid
    this.data.formatMessage.recevierUid = parseInt(this.data.uid)
    this.data.formatMessage.message = this.data.message
    this.data.formatMessage.sendTime = " "+time
    this.data.formatMessage.isRead = 0
    var timestamp = Date.parse(new Date());
    var lastSendTime = this.data.messageList.length != 0?this.data.messageList[this.data.messageList.length - 1].sendTime:"*";
    if (lastSendTime.charCodeAt(0) != 32) {
      this.data.formatMessage.isShowTime = 1
    } else {
      timestamp = timestamp - 1000 * 60 * 5
      var fiveMinAgo = new Date(timestamp)
      var hour = fiveMinAgo.getHours() < 10 ? ("0" + fiveMinAgo.getHours()) : fiveMinAgo.getHours()
      var min = fiveMinAgo.getMinutes() < 10 ? ("0" + fiveMinAgo.getMinutes()) : fiveMinAgo.getMinutes()
      var sec = fiveMinAgo.getSeconds() < 10 ? ("0" + fiveMinAgo.getSeconds()) : fiveMinAgo.getSeconds()
      var fiveMinAgoTimeStr = " " + hour + ":" + min + ":" + sec
      if (fiveMinAgoTimeStr < lastSendTime) {
        this.data.formatMessage.isShowTime = 0
      } else {
        this.data.formatMessage.isShowTime = 1
      }
    }
    this.setData({
      messageList: wx.getStorageSync('messageList')
    })
    this.data.messageList.push(this.data.formatMessage)
    this.setData({
      message: '',
      messageList: this.data.messageList
    })
    wx.setStorageSync('messageList', this.data.messageList)
    this.setData({
      toView: 'msg-' + (wx.getStorageSync('messageList').length - 1)
    })
  },
  closeKeyBoard(){
    wx.hideKeyboard()
    this.setData({
      focus:false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.onSocketMessage(async function (res) {
      console.log("收到服务器内容：" + res.data)
      that.data.formatMessage = JSON.parse(res.data)
      if (res.data != '') {
        that.setData({
          messageList: wx.getStorageSync('messageList')
        })
        that.data.messageList.push(that.data.formatMessage)
        that.setData({
          messageList: that.data.messageList
        })
        wx.setStorageSync('messageList', that.data.messageList)
        await wx.User.readMessages({
          data: {
            senderUid: that.data.senderUid,
            recevierUid: that.data.uid
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})